﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Food
{
    public class FoodServing:IFoodServing
    {
        public void AddFoodServing(Model.FoodServing serving, ref Model.NutriLeafDBEntities context)
        {
            context.FoodServings.AddObject(serving);
            context.SaveChanges();
        }

        public void AddFoodServing(int foodId, string description, float metricServingAmount, string metricServingUnit, float numberOfUnits, string measurementDescription, int nutritionId, ref Model.NutriLeafDBEntities context)
        {
            Model.FoodServing ser = new Model.FoodServing();
            ser.DateTimeAdded = DateTime.Now;
            ser.DateTimeUpdated = DateTime.Now;
            ser.FoodID = foodId;
            ser.Description = description;
            ser.MetricServingAmount = new decimal(metricServingAmount);
            ser.MetricServingUnit = metricServingUnit;
            ser.NumberOfUnits = new decimal(numberOfUnits);
         
           // error ser.MeasurementDescription =measurementDescription; 
            ser.NutritionInfoID = nutritionId;
            context.FoodServings.AddObject(ser);
            context.SaveChanges();

        }

        public void RemoveFoodServing(int foodServingId, ref Model.NutriLeafDBEntities context)
        {
            if (context.FoodServings.Where(p => p.FoodServingID == foodServingId).Count() != 0) {

                context.FoodServings.DeleteObject(context.FoodServings.Where(p => p.FoodServingID == foodServingId).First());
                context.SaveChanges();
            }
        }

        public List<Model.FoodServing> GetFoodServingFromFood(int foodId, ref Model.NutriLeafDBEntities context)
        {
          return  context.FoodServings.Where(p => p.FoodID == foodId).ToList();
        }

        public void UpdateFoodServing(Model.FoodServing foodserving, ref Model.NutriLeafDBEntities context)
        {
            throw new NotImplementedException();
        }

        public Model.FoodServing GetFoodServing(int foodServingId, ref Model.NutriLeafDBEntities context)
        {
            return context.FoodServings.Where(p => p.FoodServingID==foodServingId).First();
        }
    }
}